package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.pojo.Orders;
import com.itheima.reggie.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 订单管理
 */
@RestController
@RequestMapping("order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("submit")
    public R<String> submitOrder(@RequestBody Orders orders){
        ordersService.submitOrder(orders);
        return R.success("下单成功");
    }

    /**
     * 后台端:查询订单列表
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("page")
    public R<Page<Orders>> page(Integer page,Integer pageSize){
        //1.创建一个page对象
        Page<Orders> pageInfo=new Page<>(page,pageSize);
        //2.调用service方法,分页查询数据
        ordersService.lambdaQuery().orderByDesc(Orders::getCheckoutTime).page(pageInfo);
        //3.响应数据
        return R.success(pageInfo);
    }


    /**
     * 移动端-订单列表
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("userPage")
    public R<Page<OrdersDto>> userPage(Integer page,Integer pageSize){
        //调用业务层查询订单信息（包括订单详情项）
        Page<OrdersDto> pageInfo = ordersService.userPage(page, pageSize);
        return R.success(pageInfo);
    }

    /**
     * 后台端修改状态
     * @param orders
     * @return
     */
    @PutMapping
    public R<String> order(@RequestBody Orders orders){
        ordersService.lambdaUpdate()
                .eq(Orders::getId,orders.getId())
                .set(Orders::getStatus,orders.getStatus())
                .update();
        return R.success("修改成功");
    }

}
